"""
---------------------------------------------------------------------
- File: roles.py
- Date: 2025/11/5
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 系统角色相关API
---------------------------------------------------------------------
"""

from fastapi import APIRouter, Depends, HTTPException, Path, Body, Query

from app.core.dependency import get_service_dependency
from app.core.response import ResponseBase, ResponseSuccess, standard_response
from app.models.system import Roles
from app.schemas.system.roles import RoleCreate, RoleUpdate, RoleRead, RoleWithUser, RoleReadList
from app.service.service_roles import ServiceRoles

ServiceRoles = get_service_dependency(ServiceRoles)


roles_router = APIRouter(
    prefix="/system/roles",
    tags=["系统管理-角色管理"],
    # dependencies=[Depends(get_current_user)],
    responses=standard_response,
)


@roles_router.get("/role_users", summary="获取角色与用户信息列表", response_model=ResponseBase[list[RoleWithUser]])
async def get_role_list(
    page: int = Query(1, ge=1, description="页码"),
    size: int = Query(3, le=50, description="每页数量"),
    service_roles: ServiceRoles = Depends(ServiceRoles),
):
    """
    返回角色信息及与该角色关联的用户信息
    """
    roles = await service_roles.get_multiWithUser(skip=(page - 1) * size, limit=size)
    if not roles:
        roles = []
    return ResponseSuccess(data=roles)


@roles_router.get("/{role_id}", summary="获取某一角色信息", response_model=ResponseBase[RoleRead])
async def get_role_info(role_id: int = Path(description="角色ID"), service_roles: ServiceRoles = Depends(ServiceRoles)):
    """
    根据角色ID获取角色信息
    """
    role = await service_roles.get(id=role_id)
    if not role:
        raise HTTPException(status_code=404, detail="角色不存在")
    return ResponseSuccess(data=role)


@roles_router.get("/", summary="获取角色列表", response_model=ResponseBase[RoleReadList])
async def get_role_list(
    page: int = Query(1, ge=1, description="页码"),
    size: int = Query(3, description="每页数量"),
    filter_value: str = Query("", description="角色名称", alias="filter"),
    service_roles: ServiceRoles = Depends(ServiceRoles),
):
    """
    获取角色信息列表
    """
    print("filter_value:", filter_value)
    total, result = await service_roles.get_multi(skip=(page - 1) * size, limit=size, filter_value=filter_value)
    if not result:
        result = []
    data = RoleReadList(list=result, total=total)
    return ResponseSuccess(data=data)


@roles_router.post("/", summary="创建新角色", response_model=ResponseBase[RoleRead])
async def create_role(
    role: RoleCreate = Body(description="创建角色信息"), service_roles: ServiceRoles = Depends(ServiceRoles)
):
    """
    创建新角色
    """
    role = await service_roles.create(obj_in=role)
    return ResponseSuccess(data=role)


@roles_router.patch("/{role_id}", summary="更新角色信息", response_model=ResponseBase[RoleRead])
async def update_role(
    role_in: RoleUpdate, role_id: int = Path(description="角色ID"), service_roles: ServiceRoles = Depends(ServiceRoles)
):
    """
    根据角色ID更新角色信息
    """
    role = await service_roles.get(id=role_id)
    if not role:
        raise HTTPException(status_code=404, detail="角色不存在")
    updated_role = await service_roles.update(db_obj=role, obj_in=role_in)
    if not updated_role:
        raise HTTPException(status_code=400, detail="更新角色失败")
    return ResponseSuccess(data=updated_role)


@roles_router.delete("/{role_id}", summary="删除角色", response_model=ResponseBase[RoleRead])
async def delete_role(role_id: int = Path(description="角色ID"), service_roles: ServiceRoles = Depends(ServiceRoles)):
    """
    根据角色ID删除角色
    """
    role: Roles = await service_roles.get(id=role_id)
    if not role:
        raise HTTPException(status_code=404, detail="角色不存在")
    if role.role_no == "admin":
        raise HTTPException(status_code=400, detail="不能删除管理员角色")
    result = await service_roles.remove(id=role_id)
    return ResponseSuccess(data=result)
